home *** CD-ROM | disk | FTP | other *** search
/ Clickx 96 / Clickx 96.iso / software / tools / tool / xbmc-10.1.exe / system / python / Lib / locale.py < prev    next >
Encoding:
Python Source  |  2011-03-08  |  35.8 KB  |  932 lines

  1. """ Locale support.
  2.  
  3.     The module provides low-level access to the C lib's locale APIs
  4.     and adds high level number formatting APIs as well as a locale
  5.     aliasing engine to complement these.
  6.  
  7.     The aliasing engine includes support for many commonly used locale
  8.     names and maps them to values suitable for passing to the C lib's
  9.     setlocale() function. It also includes default encodings for all
  10.     supported locale names.
  11.  
  12. """
  13.  
  14. import sys
  15.  
  16. # Try importing the _locale module.
  17. #
  18. # If this fails, fall back on a basic 'C' locale emulation.
  19.  
  20. # Yuck:  LC_MESSAGES is non-standard:  can't tell whether it exists before
  21. # trying the import.  So __all__ is also fiddled at the end of the file.
  22. __all__ = ["setlocale","Error","localeconv","strcoll","strxfrm",
  23.            "format","str","atof","atoi","LC_CTYPE","LC_COLLATE",
  24.            "LC_TIME","LC_MONETARY","LC_NUMERIC", "LC_ALL","CHAR_MAX"]
  25.  
  26. try:
  27.  
  28.     from _locale import *
  29.  
  30. except ImportError:
  31.  
  32.     # Locale emulation
  33.  
  34.     CHAR_MAX = 127
  35.     LC_ALL = 6
  36.     LC_COLLATE = 3
  37.     LC_CTYPE = 0
  38.     LC_MESSAGES = 5
  39.     LC_MONETARY = 4
  40.     LC_NUMERIC = 1
  41.     LC_TIME = 2
  42.     Error = ValueError
  43.  
  44.     def localeconv():
  45.         """ localeconv() -> dict.
  46.             Returns numeric and monetary locale-specific parameters.
  47.         """
  48.         # 'C' locale default values
  49.         return {'grouping': [127],
  50.                 'currency_symbol': '',
  51.                 'n_sign_posn': 127,
  52.                 'p_cs_precedes': 127,
  53.                 'n_cs_precedes': 127,
  54.                 'mon_grouping': [],
  55.                 'n_sep_by_space': 127,
  56.                 'decimal_point': '.',
  57.                 'negative_sign': '',
  58.                 'positive_sign': '',
  59.                 'p_sep_by_space': 127,
  60.                 'int_curr_symbol': '',
  61.                 'p_sign_posn': 127,
  62.                 'thousands_sep': '',
  63.                 'mon_thousands_sep': '',
  64.                 'frac_digits': 127,
  65.                 'mon_decimal_point': '',
  66.                 'int_frac_digits': 127}
  67.  
  68.     def setlocale(category, value=None):
  69.         """ setlocale(integer,string=None) -> string.
  70.             Activates/queries locale processing.
  71.         """
  72.         if value not in (None, '', 'C'):
  73.             raise Error, '_locale emulation only supports "C" locale'
  74.         return 'C'
  75.  
  76.     def strcoll(a,b):
  77.         """ strcoll(string,string) -> int.
  78.             Compares two strings according to the locale.
  79.         """
  80.         return cmp(a,b)
  81.  
  82.     def strxfrm(s):
  83.         """ strxfrm(string) -> string.
  84.             Returns a string that behaves for cmp locale-aware.
  85.         """
  86.         return s
  87.  
  88. ### Number formatting APIs
  89.  
  90. # Author: Martin von Loewis
  91.  
  92. #perform the grouping from right to left
  93. def _group(s):
  94.     conv=localeconv()
  95.     grouping=conv['grouping']
  96.     if not grouping:return (s, 0)
  97.     result=""
  98.     seps = 0
  99.     spaces = ""
  100.     if s[-1] == ' ':
  101.         sp = s.find(' ')
  102.         spaces = s[sp:]
  103.         s = s[:sp]
  104.     while s and grouping:
  105.         # if grouping is -1, we are done
  106.         if grouping[0]==CHAR_MAX:
  107.             break
  108.         # 0: re-use last group ad infinitum
  109.         elif grouping[0]!=0:
  110.             #process last group
  111.             group=grouping[0]
  112.             grouping=grouping[1:]
  113.         if result:
  114.             result=s[-group:]+conv['thousands_sep']+result
  115.             seps += 1
  116.         else:
  117.             result=s[-group:]
  118.         s=s[:-group]
  119.         if s and s[-1] not in "0123456789":
  120.             # the leading string is only spaces and signs
  121.             return s+result+spaces,seps
  122.     if not result:
  123.         return s+spaces,seps
  124.     if s:
  125.         result=s+conv['thousands_sep']+result
  126.         seps += 1
  127.     return result+spaces,seps
  128.  
  129. def format(f,val,grouping=0):
  130.     """Formats a value in the same way that the % formatting would use,
  131.     but takes the current locale into account.
  132.     Grouping is applied if the third parameter is true."""
  133.     result = f % val
  134.     fields = result.split(".")
  135.     seps = 0
  136.     if grouping:
  137.         fields[0],seps=_group(fields[0])
  138.     if len(fields)==2:
  139.         result = fields[0]+localeconv()['decimal_point']+fields[1]
  140.     elif len(fields)==1:
  141.         result = fields[0]
  142.     else:
  143.         raise Error, "Too many decimal points in result string"
  144.  
  145.     while seps:
  146.         # If the number was formatted for a specific width, then it
  147.         # might have been filled with spaces to the left or right. If
  148.         # so, kill as much spaces as there where separators.
  149.         # Leading zeroes as fillers are not yet dealt with, as it is
  150.         # not clear how they should interact with grouping.
  151.         sp = result.find(" ")
  152.         if sp==-1:break
  153.         result = result[:sp]+result[sp+1:]
  154.         seps -= 1
  155.  
  156.     return result
  157.  
  158. def str(val):
  159.     """Convert float to integer, taking the locale into account."""
  160.     return format("%.12g",val)
  161.  
  162. def atof(string,func=float):
  163.     "Parses a string as a float according to the locale settings."
  164.     #First, get rid of the grouping
  165.     ts = localeconv()['thousands_sep']
  166.     if ts:
  167.         string = string.replace(ts, '')
  168.     #next, replace the decimal point with a dot
  169.     dd = localeconv()['decimal_point']
  170.     if dd:
  171.         string = string.replace(dd, '.')
  172.     #finally, parse the string
  173.     return func(string)
  174.  
  175. def atoi(str):
  176.     "Converts a string to an integer according to the locale settings."
  177.     return atof(str, int)
  178.  
  179. def _test():
  180.     setlocale(LC_ALL, "")
  181.     #do grouping
  182.     s1=format("%d", 123456789,1)
  183.     print s1, "is", atoi(s1)
  184.     #standard formatting
  185.     s1=str(3.14)
  186.     print s1, "is", atof(s1)
  187.  
  188. ### Locale name aliasing engine
  189.  
  190. # Author: Marc-Andre Lemburg, mal@lemburg.com
  191. # Various tweaks by Fredrik Lundh <fredrik@pythonware.com>
  192.  
  193. # store away the low-level version of setlocale (it's
  194. # overridden below)
  195. _setlocale = setlocale
  196.  
  197. def normalize(localename):
  198.  
  199.     """ Returns a normalized locale code for the given locale
  200.         name.
  201.  
  202.         The returned locale code is formatted for use with
  203.         setlocale().
  204.  
  205.         If normalization fails, the original name is returned
  206.         unchanged.
  207.  
  208.         If the given encoding is not known, the function defaults to
  209.         the default encoding for the locale code just like setlocale()
  210.         does.
  211.  
  212.     """
  213.     # Normalize the locale name and extract the encoding
  214.     fullname = localename.lower()
  215.     if ':' in fullname:
  216.         # ':' is sometimes used as encoding delimiter.
  217.         fullname = fullname.replace(':', '.')
  218.     if '.' in fullname:
  219.         langname, encoding = fullname.split('.')[:2]
  220.         fullname = langname + '.' + encoding
  221.     else:
  222.         langname = fullname
  223.         encoding = ''
  224.  
  225.     # First lookup: fullname (possibly with encoding)
  226.     code = locale_alias.get(fullname, None)
  227.     if code is not None:
  228.         return code
  229.  
  230.     # Second try: langname (without encoding)
  231.     code = locale_alias.get(langname, None)
  232.     if code is not None:
  233.         if '.' in code:
  234.             langname, defenc = code.split('.')
  235.         else:
  236.             langname = code
  237.             defenc = ''
  238.         if encoding:
  239.             encoding = encoding_alias.get(encoding, encoding)
  240.         else:
  241.             encoding = defenc
  242.         if encoding:
  243.             return langname + '.' + encoding
  244.         else:
  245.             return langname
  246.  
  247.     else:
  248.         return localename
  249.  
  250. def _parse_localename(localename):
  251.  
  252.     """ Parses the locale code for localename and returns the
  253.         result as tuple (language code, encoding).
  254.  
  255.         The localename is normalized and passed through the locale
  256.         alias engine. A ValueError is raised in case the locale name
  257.         cannot be parsed.
  258.  
  259.         The language code corresponds to RFC 1766.  code and encoding
  260.         can be None in case the values cannot be determined or are
  261.         unknown to this implementation.
  262.  
  263.     """
  264.     code = normalize(localename)
  265.     if '@' in code:
  266.         # Deal with locale modifiers
  267.         code, modifier = code.split('@')
  268.         if modifier == 'euro' and '.' not in code:
  269.             # Assume Latin-9 for @euro locales. This is bogus,
  270.             # since some systems may use other encodings for these
  271.             # locales. Also, we ignore other modifiers.
  272.             return code, 'iso-8859-15'
  273.  
  274.     if '.' in code:
  275.         return tuple(code.split('.')[:2])
  276.     elif code == 'C':
  277.         return None, None
  278.     raise ValueError, 'unknown locale: %s' % localename
  279.  
  280. def _build_localename(localetuple):
  281.  
  282.     """ Builds a locale code from the given tuple (language code,
  283.         encoding).
  284.  
  285.         No aliasing or normalizing takes place.
  286.  
  287.     """
  288.     language, encoding = localetuple
  289.     if language is None:
  290.         language = 'C'
  291.     if encoding is None:
  292.         return language
  293.     else:
  294.         return language + '.' + encoding
  295.  
  296. def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
  297.  
  298.     """ Tries to determine the default locale settings and returns
  299.         them as tuple (language code, encoding).
  300.  
  301.         According to POSIX, a program which has not called
  302.         setlocale(LC_ALL, "") runs using the portable 'C' locale.
  303.         Calling setlocale(LC_ALL, "") lets it use the default locale as
  304.         defined by the LANG variable. Since we don't want to interfere
  305.         with the current locale setting we thus emulate the behavior
  306.         in the way described above.
  307.  
  308.         To maintain compatibility with other platforms, not only the
  309.         LANG variable is tested, but a list of variables given as
  310.         envvars parameter. The first found to be defined will be
  311.         used. envvars defaults to the search path used in GNU gettext;
  312.         it must always contain the variable name 'LANG'.
  313.  
  314.         Except for the code 'C', the language code corresponds to RFC
  315.         1766.  code and encoding can be None in case the values cannot
  316.         be determined.
  317.  
  318.     """
  319.  
  320.     try:
  321.         # check if it's supported by the _locale module
  322.         import _locale
  323.         code, encoding = _locale._getdefaultlocale()
  324.     except (ImportError, AttributeError):
  325.         pass
  326.     else:
  327.         # make sure the code/encoding values are valid
  328.         if sys.platform == "win32" and code and code[:2] == "0x":
  329.             # map windows language identifier to language name
  330.             code = windows_locale.get(int(code, 0))
  331.         # ...add other platform-specific processing here, if
  332.         # necessary...
  333.         return code, encoding
  334.  
  335.     # fall back on POSIX behaviour
  336.     import os
  337.     lookup = os.environ.get
  338.     for variable in envvars:
  339.         localename = lookup(variable,None)
  340.         if localename:
  341.             if variable == 'LANGUAGE':
  342.                 localename = localename.split(':')[0]
  343.             break
  344.     else:
  345.         localename = 'C'
  346.     return _parse_localename(localename)
  347.  
  348.  
  349. def getlocale(category=LC_CTYPE):
  350.  
  351.     """ Returns the current setting for the given locale category as
  352.         tuple (language code, encoding).
  353.  
  354.         category may be one of the LC_* value except LC_ALL. It
  355.         defaults to LC_CTYPE.
  356.  
  357.         Except for the code 'C', the language code corresponds to RFC
  358.         1766.  code and encoding can be None in case the values cannot
  359.         be determined.
  360.  
  361.     """
  362.     localename = _setlocale(category)
  363.     if category == LC_ALL and ';' in localename:
  364.         raise TypeError, 'category LC_ALL is not supported'
  365.     return _parse_localename(localename)
  366.  
  367. def setlocale(category, locale=None):
  368.  
  369.     """ Set the locale for the given category.  The locale can be
  370.         a string, a locale tuple (language code, encoding), or None.
  371.  
  372.         Locale tuples are converted to strings the locale aliasing
  373.         engine.  Locale strings are passed directly to the C lib.
  374.  
  375.         category may be given as one of the LC_* values.
  376.  
  377.     """
  378.     if locale and type(locale) is not type(""):
  379.         # convert to string
  380.         locale = normalize(_build_localename(locale))
  381.     return _setlocale(category, locale)
  382.  
  383. def resetlocale(category=LC_ALL):
  384.  
  385.     """ Sets the locale for category to the default setting.
  386.  
  387.         The default setting is determined by calling
  388.         getdefaultlocale(). category defaults to LC_ALL.
  389.  
  390.     """
  391.     _setlocale(category, _build_localename(getdefaultlocale()))
  392.  
  393. if sys.platform in ('win32', 'darwin', 'mac'):
  394.     # On Win32, this will return the ANSI code page
  395.     # On the Mac, it should return the system encoding;
  396.     # it might return "ascii" instead
  397.     def getpreferredencoding(do_setlocale = True):
  398.         """Return the charset that the user is likely using."""
  399.         import _locale
  400.         return _locale._getdefaultlocale()[1]
  401. else:
  402.     # On Unix, if CODESET is available, use that.
  403.     try:
  404.         CODESET
  405.     except NameError:
  406.         # Fall back to parsing environment variables :-(
  407.         def getpreferredencoding(do_setlocale = True):
  408.             """Return the charset that the user is likely using,
  409.             by looking at environment variables."""
  410.             return getdefaultlocale()[1]
  411.     else:
  412.         def getpreferredencoding(do_setlocale = True):
  413.             """Return the charset that the user is likely using,
  414.             according to the system configuration."""
  415.             if do_setlocale:
  416.                 oldloc = setlocale(LC_CTYPE)
  417.                 setlocale(LC_CTYPE, "")
  418.                 result = nl_langinfo(CODESET)
  419.                 setlocale(LC_CTYPE, oldloc)
  420.                 return result
  421.             else:
  422.                 return nl_langinfo(CODESET)
  423.  
  424.  
  425. ### Database
  426. #
  427. # The following data was extracted from the locale.alias file which
  428. # comes with X11 and then hand edited removing the explicit encoding
  429. # definitions and adding some more aliases. The file is usually
  430. # available as /usr/lib/X11/locale/locale.alias.
  431. #
  432.  
  433. #
  434. # The encoding_alias table maps lowercase encoding alias names to C
  435. # locale encoding names (case-sensitive).
  436. #
  437. encoding_alias = {
  438.         '437':                          'C',
  439.         'c':                            'C',
  440.         'iso8859':                      'ISO8859-1',
  441.         '8859':                         'ISO8859-1',
  442.         '88591':                        'ISO8859-1',
  443.         'ascii':                        'ISO8859-1',
  444.         'en':                           'ISO8859-1',
  445.         'iso88591':                     'ISO8859-1',
  446.         'iso_8859-1':                   'ISO8859-1',
  447.         '885915':                       'ISO8859-15',
  448.         'iso885915':                    'ISO8859-15',
  449.         'iso_8859-15':                  'ISO8859-15',
  450.         'iso8859-2':                    'ISO8859-2',
  451.         'iso88592':                     'ISO8859-2',
  452.         'iso_8859-2':                   'ISO8859-2',
  453.         'iso88595':                     'ISO8859-5',
  454.         'iso88596':                     'ISO8859-6',
  455.         'iso88597':                     'ISO8859-7',
  456.         'iso88598':                     'ISO8859-8',
  457.         'iso88599':                     'ISO8859-9',
  458.         'iso-2022-jp':                  'JIS7',
  459.         'jis':                          'JIS7',
  460.         'jis7':                         'JIS7',
  461.         'sjis':                         'SJIS',
  462.         'tis620':                       'TACTIS',
  463.         'ajec':                         'eucJP',
  464.         'eucjp':                        'eucJP',
  465.         'ujis':                         'eucJP',
  466.         'utf-8':                        'utf',
  467.         'utf8':                         'utf',
  468.         'utf8@ucs4':                    'utf',
  469. }
  470.  
  471. #
  472. # The locale_alias table maps lowercase alias names to C locale names
  473. # (case-sensitive). Encodings are always separated from the locale
  474. # name using a dot ('.'); they should only be given in case the
  475. # language name is needed to interpret the given encoding alias
  476. # correctly (CJK codes often have this need).
  477. #
  478. locale_alias = {
  479.         'american':                      'en_US.ISO8859-1',
  480.         'ar':                            'ar_AA.ISO8859-6',
  481.         'ar_aa':                         'ar_AA.ISO8859-6',
  482.         'ar_sa':                         'ar_SA.ISO8859-6',
  483.         'arabic':                        'ar_AA.ISO8859-6',
  484.         'bg':                            'bg_BG.ISO8859-5',
  485.         'bg_bg':                         'bg_BG.ISO8859-5',
  486.         'bulgarian':                     'bg_BG.ISO8859-5',
  487.         'c-french':                      'fr_CA.ISO8859-1',
  488.         'c':                             'C',
  489.         'c_c':                           'C',
  490.         'cextend':                       'en_US.ISO8859-1',
  491.         'chinese-s':                     'zh_CN.eucCN',
  492.         'chinese-t':                     'zh_TW.eucTW',
  493.         'croatian':                      'hr_HR.ISO8859-2',
  494.         'cs':                            'cs_CZ.ISO8859-2',
  495.         'cs_cs':                         'cs_CZ.ISO8859-2',
  496.         'cs_cz':                         'cs_CZ.ISO8859-2',
  497.         'cz':                            'cz_CZ.ISO8859-2',
  498.         'cz_cz':                         'cz_CZ.ISO8859-2',
  499.         'czech':                         'cs_CS.ISO8859-2',
  500.         'da':                            'da_DK.ISO8859-1',
  501.         'da_dk':                         'da_DK.ISO8859-1',
  502.         'danish':                        'da_DK.ISO8859-1',
  503.         'de':                            'de_DE.ISO8859-1',
  504.         'de_at':                         'de_AT.ISO8859-1',
  505.         'de_ch':                         'de_CH.ISO8859-1',
  506.         'de_de':                         'de_DE.ISO8859-1',
  507.         'dutch':                         'nl_BE.ISO8859-1',
  508.         'ee':                            'ee_EE.ISO8859-4',
  509.         'el':                            'el_GR.ISO8859-7',
  510.         'el_gr':                         'el_GR.ISO8859-7',
  511.         'en':                            'en_US.ISO8859-1',
  512.         'en_au':                         'en_AU.ISO8859-1',
  513.         'en_ca':                         'en_CA.ISO8859-1',
  514.         'en_gb':                         'en_GB.ISO8859-1',
  515.         'en_ie':                         'en_IE.ISO8859-1',
  516.         'en_nz':                         'en_NZ.ISO8859-1',
  517.         'en_uk':                         'en_GB.ISO8859-1',
  518.         'en_us':                         'en_US.ISO8859-1',
  519.         'eng_gb':                        'en_GB.ISO8859-1',
  520.         'english':                       'en_EN.ISO8859-1',
  521.         'english_uk':                    'en_GB.ISO8859-1',
  522.         'english_united-states':         'en_US.ISO8859-1',
  523.         'english_us':                    'en_US.ISO8859-1',
  524.         'es':                            'es_ES.ISO8859-1',
  525.         'es_ar':                         'es_AR.ISO8859-1',
  526.         'es_bo':                         'es_BO.ISO8859-1',
  527.         'es_cl':                         'es_CL.ISO8859-1',
  528.         'es_co':                         'es_CO.ISO8859-1',
  529.         'es_cr':                         'es_CR.ISO8859-1',
  530.         'es_ec':                         'es_EC.ISO8859-1',
  531.         'es_es':                         'es_ES.ISO8859-1',
  532.         'es_gt':                         'es_GT.ISO8859-1',
  533.         'es_mx':                         'es_MX.ISO8859-1',
  534.         'es_ni':                         'es_NI.ISO8859-1',
  535.         'es_pa':                         'es_PA.ISO8859-1',
  536.         'es_pe':                         'es_PE.ISO8859-1',
  537.         'es_py':                         'es_PY.ISO8859-1',
  538.         'es_sv':                         'es_SV.ISO8859-1',
  539.         'es_uy':                         'es_UY.ISO8859-1',
  540.         'es_ve':                         'es_VE.ISO8859-1',
  541.         'et':                            'et_EE.ISO8859-4',
  542.         'et_ee':                         'et_EE.ISO8859-4',
  543.         'fi':                            'fi_FI.ISO8859-1',
  544.         'fi_fi':                         'fi_FI.ISO8859-1',
  545.         'finnish':                       'fi_FI.ISO8859-1',
  546.         'fr':                            'fr_FR.ISO8859-1',
  547.         'fr_be':                         'fr_BE.ISO8859-1',
  548.         'fr_ca':                         'fr_CA.ISO8859-1',
  549.         'fr_ch':                         'fr_CH.ISO8859-1',
  550.         'fr_fr':                         'fr_FR.ISO8859-1',
  551.         'fre_fr':                        'fr_FR.ISO8859-1',
  552.         'french':                        'fr_FR.ISO8859-1',
  553.         'french_france':                 'fr_FR.ISO8859-1',
  554.         'ger_de':                        'de_DE.ISO8859-1',
  555.         'german':                        'de_DE.ISO8859-1',
  556.         'german_germany':                'de_DE.ISO8859-1',
  557.         'greek':                         'el_GR.ISO8859-7',
  558.         'hebrew':                        'iw_IL.ISO8859-8',
  559.         'hr':                            'hr_HR.ISO8859-2',
  560.         'hr_hr':                         'hr_HR.ISO8859-2',
  561.         'hu':                            'hu_HU.ISO8859-2',
  562.         'hu_hu':                         'hu_HU.ISO8859-2',
  563.         'hungarian':                     'hu_HU.ISO8859-2',
  564.         'icelandic':                     'is_IS.ISO8859-1',
  565.         'id':                            'id_ID.ISO8859-1',
  566.         'id_id':                         'id_ID.ISO8859-1',
  567.         'is':                            'is_IS.ISO8859-1',
  568.         'is_is':                         'is_IS.ISO8859-1',
  569.         'iso-8859-1':                    'en_US.ISO8859-1',
  570.         'iso-8859-15':                   'en_US.ISO8859-15',
  571.         'iso8859-1':                     'en_US.ISO8859-1',
  572.         'iso8859-15':                    'en_US.ISO8859-15',
  573.         'iso_8859_1':                    'en_US.ISO8859-1',
  574.         'iso_8859_15':                   'en_US.ISO8859-15',
  575.         'it':                            'it_IT.ISO8859-1',
  576.         'it_ch':                         'it_CH.ISO8859-1',
  577.         'it_it':                         'it_IT.ISO8859-1',
  578.         'italian':                       'it_IT.ISO8859-1',
  579.         'iw':                            'iw_IL.ISO8859-8',
  580.         'iw_il':                         'iw_IL.ISO8859-8',
  581.         'ja':                            'ja_JP.eucJP',
  582.         'ja.jis':                        'ja_JP.JIS7',
  583.         'ja.sjis':                       'ja_JP.SJIS',
  584.         'ja_jp':                         'ja_JP.eucJP',
  585.         'ja_jp.ajec':                    'ja_JP.eucJP',
  586.         'ja_jp.euc':                     'ja_JP.eucJP',
  587.         'ja_jp.eucjp':                   'ja_JP.eucJP',
  588.         'ja_jp.iso-2022-jp':             'ja_JP.JIS7',
  589.         'ja_jp.jis':                     'ja_JP.JIS7',
  590.         'ja_jp.jis7':                    'ja_JP.JIS7',
  591.         'ja_jp.mscode':                  'ja_JP.SJIS',
  592.         'ja_jp.sjis':                    'ja_JP.SJIS',
  593.         'ja_jp.ujis':                    'ja_JP.eucJP',
  594.         'japan':                         'ja_JP.eucJP',
  595.         'japanese':                      'ja_JP.SJIS',
  596.         'japanese-euc':                  'ja_JP.eucJP',
  597.         'japanese.euc':                  'ja_JP.eucJP',
  598.         'jp_jp':                         'ja_JP.eucJP',
  599.         'ko':                            'ko_KR.eucKR',
  600.         'ko_kr':                         'ko_KR.eucKR',
  601.         'ko_kr.euc':                     'ko_KR.eucKR',
  602.         'korean':                        'ko_KR.eucKR',
  603.         'lt':                            'lt_LT.ISO8859-4',
  604.         'lv':                            'lv_LV.ISO8859-4',
  605.         'mk':                            'mk_MK.ISO8859-5',
  606.         'mk_mk':                         'mk_MK.ISO8859-5',
  607.         'nl':                            'nl_NL.ISO8859-1',
  608.         'nl_be':                         'nl_BE.ISO8859-1',
  609.         'nl_nl':                         'nl_NL.ISO8859-1',
  610.         'no':                            'no_NO.ISO8859-1',
  611.         'no_no':                         'no_NO.ISO8859-1',
  612.         'norwegian':                     'no_NO.ISO8859-1',
  613.         'pl':                            'pl_PL.ISO8859-2',
  614.         'pl_pl':                         'pl_PL.ISO8859-2',
  615.         'polish':                        'pl_PL.ISO8859-2',
  616.         'portuguese':                    'pt_PT.ISO8859-1',
  617.         'portuguese_brazil':             'pt_BR.ISO8859-1',
  618.         'posix':                         'C',
  619.         'posix-utf2':                    'C',
  620.         'pt':                            'pt_PT.ISO8859-1',
  621.         'pt_br':                         'pt_BR.ISO8859-1',
  622.         'pt_pt':                         'pt_PT.ISO8859-1',
  623.         'ro':                            'ro_RO.ISO8859-2',
  624.         'ro_ro':                         'ro_RO.ISO8859-2',
  625.         'ru':                            'ru_RU.ISO8859-5',
  626.         'ru_ru':                         'ru_RU.ISO8859-5',
  627.         'rumanian':                      'ro_RO.ISO8859-2',
  628.         'russian':                       'ru_RU.ISO8859-5',
  629.         'serbocroatian':                 'sh_YU.ISO8859-2',
  630.         'sh':                            'sh_YU.ISO8859-2',
  631.         'sh_hr':                         'sh_HR.ISO8859-2',
  632.         'sh_sp':                         'sh_YU.ISO8859-2',
  633.         'sh_yu':                         'sh_YU.ISO8859-2',
  634.         'sk':                            'sk_SK.ISO8859-2',
  635.         'sk_sk':                         'sk_SK.ISO8859-2',
  636.         'sl':                            'sl_CS.ISO8859-2',
  637.         'sl_cs':                         'sl_CS.ISO8859-2',
  638.         'sl_si':                         'sl_SI.ISO8859-2',
  639.         'slovak':                        'sk_SK.ISO8859-2',
  640.         'slovene':                       'sl_CS.ISO8859-2',
  641.         'sp':                            'sp_YU.ISO8859-5',
  642.         'sp_yu':                         'sp_YU.ISO8859-5',
  643.         'spanish':                       'es_ES.ISO8859-1',
  644.         'spanish_spain':                 'es_ES.ISO8859-1',
  645.         'sr_sp':                         'sr_SP.ISO8859-2',
  646.         'sv':                            'sv_SE.ISO8859-1',
  647.         'sv_se':                         'sv_SE.ISO8859-1',
  648.         'swedish':                       'sv_SE.ISO8859-1',
  649.         'th_th':                         'th_TH.TACTIS',
  650.         'tr':                            'tr_TR.ISO8859-9',
  651.         'tr_tr':                         'tr_TR.ISO8859-9',
  652.         'turkish':                       'tr_TR.ISO8859-9',
  653.         'univ':                          'en_US.utf',
  654.         'universal':                     'en_US.utf',
  655.         'zh':                            'zh_CN.eucCN',
  656.         'zh_cn':                         'zh_CN.eucCN',
  657.         'zh_cn.big5':                    'zh_TW.eucTW',
  658.         'zh_cn.euc':                     'zh_CN.eucCN',
  659.         'zh_tw':                         'zh_TW.eucTW',
  660.         'zh_tw.euc':                     'zh_TW.eucTW',
  661. }
  662.  
  663. #
  664. # This maps Windows language identifiers to locale strings.
  665. #
  666. # This list has been updated from
  667. # http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/nls_238z.asp
  668. # to include every locale up to Windows XP.
  669. #
  670. # NOTE: this mapping is incomplete.  If your language is missing, please
  671. # submit a bug report to Python bug manager, which you can find via:
  672. #     http://www.python.org/dev/
  673. # Make sure you include the missing language identifier and the suggested
  674. # locale code.
  675. #
  676.  
  677. windows_locale = {
  678.     0x0436: "af_ZA", # Afrikaans
  679.     0x041c: "sq_AL", # Albanian
  680.     0x0401: "ar_SA", # Arabic - Saudi Arabia
  681.     0x0801: "ar_IQ", # Arabic - Iraq
  682.     0x0c01: "ar_EG", # Arabic - Egypt
  683.     0x1001: "ar_LY", # Arabic - Libya
  684.     0x1401: "ar_DZ", # Arabic - Algeria
  685.     0x1801: "ar_MA", # Arabic - Morocco
  686.     0x1c01: "ar_TN", # Arabic - Tunisia
  687.     0x2001: "ar_OM", # Arabic - Oman
  688.     0x2401: "ar_YE", # Arabic - Yemen
  689.     0x2801: "ar_SY", # Arabic - Syria
  690.     0x2c01: "ar_JO", # Arabic - Jordan
  691.     0x3001: "ar_LB", # Arabic - Lebanon
  692.     0x3401: "ar_KW", # Arabic - Kuwait
  693.     0x3801: "ar_AE", # Arabic - United Arab Emirates
  694.     0x3c01: "ar_BH", # Arabic - Bahrain
  695.     0x4001: "ar_QA", # Arabic - Qatar
  696.     0x042b: "hy_AM", # Armenian
  697.     0x042c: "az_AZ", # Azeri Latin
  698.     0x082c: "az_AZ", # Azeri - Cyrillic
  699.     0x042d: "eu_ES", # Basque
  700.     0x0423: "be_BY", # Belarusian
  701.     0x0445: "bn_IN", # Begali
  702.     0x201a: "bs_BA", # Bosnian
  703.     0x141a: "bs_BA", # Bosnian - Cyrillic
  704.     0x047e: "br_FR", # Breton - France
  705.     0x0402: "bg_BG", # Bulgarian
  706.     0x0403: "ca_ES", # Catalan
  707.     0x0004: "zh_CHS",# Chinese - Simplified
  708.     0x0404: "zh_TW", # Chinese - Taiwan
  709.     0x0804: "zh_CN", # Chinese - PRC
  710.     0x0c04: "zh_HK", # Chinese - Hong Kong S.A.R.
  711.     0x1004: "zh_SG", # Chinese - Singapore
  712.     0x1404: "zh_MO", # Chinese - Macao S.A.R.
  713.     0x7c04: "zh_CHT",# Chinese - Traditional
  714.     0x041a: "hr_HR", # Croatian
  715.     0x101a: "hr_BA", # Croatian - Bosnia
  716.     0x0405: "cs_CZ", # Czech
  717.     0x0406: "da_DK", # Danish
  718.     0x048c: "gbz_AF",# Dari - Afghanistan
  719.     0x0465: "div_MV",# Divehi - Maldives
  720.     0x0413: "nl_NL", # Dutch - The Netherlands
  721.     0x0813: "nl_BE", # Dutch - Belgium
  722.     0x0409: "en_US", # English - United States
  723.     0x0809: "en_GB", # English - United Kingdom
  724.     0x0c09: "en_AU", # English - Australia
  725.     0x1009: "en_CA", # English - Canada
  726.     0x1409: "en_NZ", # English - New Zealand
  727.     0x1809: "en_IE", # English - Ireland
  728.     0x1c09: "en_ZA", # English - South Africa
  729.     0x2009: "en_JA", # English - Jamaica
  730.     0x2409: "en_CB", # English - Carribbean
  731.     0x2809: "en_BZ", # English - Belize
  732.     0x2c09: "en_TT", # English - Trinidad
  733.     0x3009: "en_ZW", # English - Zimbabwe
  734.     0x3409: "en_PH", # English - Phillippines
  735.     0x0425: "et_EE", # Estonian
  736.     0x0438: "fo_FO", # Faroese
  737.     0x0464: "fil_PH",# Filipino
  738.     0x040b: "fi_FI", # Finnish
  739.     0x040c: "fr_FR", # French - France
  740.     0x080c: "fr_BE", # French - Belgium
  741.     0x0c0c: "fr_CA", # French - Canada
  742.     0x100c: "fr_CH", # French - Switzerland
  743.     0x140c: "fr_LU", # French - Luxembourg
  744.     0x180c: "fr_MC", # French - Monaco
  745.     0x0462: "fy_NL", # Frisian - Netherlands
  746.     0x0456: "gl_ES", # Galician
  747.     0x0437: "ka_GE", # Georgian
  748.     0x0407: "de_DE", # German - Germany
  749.     0x0807: "de_CH", # German - Switzerland
  750.     0x0c07: "de_AT", # German - Austria
  751.     0x1007: "de_LU", # German - Luxembourg
  752.     0x1407: "de_LI", # German - Liechtenstein
  753.     0x0408: "el_GR", # Greek
  754.     0x0447: "gu_IN", # Gujarati
  755.     0x040d: "he_IL", # Hebrew
  756.     0x0439: "hi_IN", # Hindi
  757.     0x040e: "hu_HU", # Hungarian
  758.     0x040f: "is_IS", # Icelandic
  759.     0x0421: "id_ID", # Indonesian
  760.     0x045d: "iu_CA", # Inuktitut
  761.     0x085d: "iu_CA", # Inuktitut - Latin
  762.     0x083c: "ga_IE", # Irish - Ireland
  763.     0x0434: "xh_ZA", # Xhosa - South Africa
  764.     0x0435: "zu_ZA", # Zulu
  765.     0x0410: "it_IT", # Italian - Italy
  766.     0x0810: "it_CH", # Italian - Switzerland
  767.     0x0411: "ja_JP", # Japanese
  768.     0x044b: "kn_IN", # Kannada - India
  769.     0x043f: "kk_KZ", # Kazakh
  770.     0x0457: "kok_IN",# Konkani
  771.     0x0412: "ko_KR", # Korean
  772.     0x0440: "ky_KG", # Kyrgyz
  773.     0x0426: "lv_LV", # Latvian
  774.     0x0427: "lt_LT", # Lithuanian
  775.     0x046e: "lb_LU", # Luxembourgish
  776.     0x042f: "mk_MK", # FYRO Macedonian
  777.     0x043e: "ms_MY", # Malay - Malaysia
  778.     0x083e: "ms_BN", # Malay - Brunei
  779.     0x044c: "ml_IN", # Malayalam - India
  780.     0x043a: "mt_MT", # Maltese
  781.     0x0481: "mi_NZ", # Maori
  782.     0x047a: "arn_CL",# Mapudungun
  783.     0x044e: "mr_IN", # Marathi
  784.     0x047c: "moh_CA",# Mohawk - Canada
  785.     0x0450: "mn_MN", # Mongolian
  786.     0x0461: "ne_NP", # Nepali
  787.     0x0414: "nb_NO", # Norwegian - Bokmal
  788.     0x0814: "nn_NO", # Norwegian - Nynorsk
  789.     0x0482: "oc_FR", # Occitan - France
  790.     0x0448: "or_IN", # Oriya - India
  791.     0x0463: "ps_AF", # Pashto - Afghanistan
  792.     0x0429: "fa_IR", # Persian
  793.     0x0415: "pl_PL", # Polish
  794.     0x0416: "pt_BR", # Portuguese - Brazil
  795.     0x0816: "pt_PT", # Portuguese - Portugal
  796.     0x0446: "pa_IN", # Punjabi
  797.     0x046b: "quz_BO",# Quechua (Bolivia)
  798.     0x086b: "quz_EC",# Quechua (Ecuador)
  799.     0x0c6b: "quz_PE",# Quechua (Peru)
  800.     0x0418: "ro_RO", # Romanian - Romania
  801.     0x0417: "rm_CH", # Raeto-Romanese
  802.     0x0419: "ru_RU", # Russian
  803.     0x243b: "smn_FI",# Sami Finland
  804.     0x103b: "smj_NO",# Sami Norway
  805.     0x143b: "smj_SE",# Sami Sweden
  806.     0x043b: "se_NO", # Sami Northern Norway
  807.     0x083b: "se_SE", # Sami Northern Sweden
  808.     0x0c3b: "se_FI", # Sami Northern Finland
  809.     0x203b: "sms_FI",# Sami Skolt
  810.     0x183b: "sma_NO",# Sami Southern Norway
  811.     0x1c3b: "sma_SE",# Sami Southern Sweden
  812.     0x044f: "sa_IN", # Sanskrit
  813.     0x0c1a: "sr_SP", # Serbian - Cyrillic
  814.     0x1c1a: "sr_BA", # Serbian - Bosnia Cyrillic
  815.     0x081a: "sr_SP", # Serbian - Latin
  816.     0x181a: "sr_BA", # Serbian - Bosnia Latin
  817.     0x046c: "ns_ZA", # Northern Sotho
  818.     0x0432: "tn_ZA", # Setswana - Southern Africa
  819.     0x041b: "sk_SK", # Slovak
  820.     0x0424: "sl_SI", # Slovenian
  821.     0x040a: "es_ES", # Spanish - Spain
  822.     0x080a: "es_MX", # Spanish - Mexico
  823.     0x0c0a: "es_ES", # Spanish - Spain (Modern)
  824.     0x100a: "es_GT", # Spanish - Guatemala
  825.     0x140a: "es_CR", # Spanish - Costa Rica
  826.     0x180a: "es_PA", # Spanish - Panama
  827.     0x1c0a: "es_DO", # Spanish - Dominican Republic
  828.     0x200a: "es_VE", # Spanish - Venezuela
  829.     0x240a: "es_CO", # Spanish - Colombia
  830.     0x280a: "es_PE", # Spanish - Peru
  831.     0x2c0a: "es_AR", # Spanish - Argentina
  832.     0x300a: "es_EC", # Spanish - Ecuador
  833.     0x340a: "es_CL", # Spanish - Chile
  834.     0x380a: "es_UR", # Spanish - Uruguay
  835.     0x3c0a: "es_PY", # Spanish - Paraguay
  836.     0x400a: "es_BO", # Spanish - Bolivia
  837.     0x440a: "es_SV", # Spanish - El Salvador
  838.     0x480a: "es_HN", # Spanish - Honduras
  839.     0x4c0a: "es_NI", # Spanish - Nicaragua
  840.     0x500a: "es_PR", # Spanish - Puerto Rico
  841.     0x0441: "sw_KE", # Swahili
  842.     0x041d: "sv_SE", # Swedish - Sweden
  843.     0x081d: "sv_FI", # Swedish - Finland
  844.     0x045a: "syr_SY",# Syriac
  845.     0x0449: "ta_IN", # Tamil
  846.     0x0444: "tt_RU", # Tatar
  847.     0x044a: "te_IN", # Telugu
  848.     0x041e: "th_TH", # Thai
  849.     0x041f: "tr_TR", # Turkish
  850.     0x0422: "uk_UA", # Ukrainian
  851.     0x0420: "ur_PK", # Urdu
  852.     0x0820: "ur_IN", # Urdu - India
  853.     0x0443: "uz_UZ", # Uzbek - Latin
  854.     0x0843: "uz_UZ", # Uzbek - Cyrillic
  855.     0x042a: "vi_VN", # Vietnamese
  856.     0x0452: "cy_GB", # Welsh
  857. }
  858.  
  859. def _print_locale():
  860.  
  861.     """ Test function.
  862.     """
  863.     categories = {}
  864.     def _init_categories(categories=categories):
  865.         for k,v in globals().items():
  866.             if k[:3] == 'LC_':
  867.                 categories[k] = v
  868.     _init_categories()
  869.     del categories['LC_ALL']
  870.  
  871.     print 'Locale defaults as determined by getdefaultlocale():'
  872.     print '-'*72
  873.     lang, enc = getdefaultlocale()
  874.     print 'Language: ', lang or '(undefined)'
  875.     print 'Encoding: ', enc or '(undefined)'
  876.     print
  877.  
  878.     print 'Locale settings on startup:'
  879.     print '-'*72
  880.     for name,category in categories.items():
  881.         print name, '...'
  882.         lang, enc = getlocale(category)
  883.         print '   Language: ', lang or '(undefined)'
  884.         print '   Encoding: ', enc or '(undefined)'
  885.         print
  886.  
  887.     print
  888.     print 'Locale settings after calling resetlocale():'
  889.     print '-'*72
  890.     resetlocale()
  891.     for name,category in categories.items():
  892.         print name, '...'
  893.         lang, enc = getlocale(category)
  894.         print '   Language: ', lang or '(undefined)'
  895.         print '   Encoding: ', enc or '(undefined)'
  896.         print
  897.  
  898.     try:
  899.         setlocale(LC_ALL, "")
  900.     except:
  901.         print 'NOTE:'
  902.         print 'setlocale(LC_ALL, "") does not support the default locale'
  903.         print 'given in the OS environment variables.'
  904.     else:
  905.         print
  906.         print 'Locale settings after calling setlocale(LC_ALL, ""):'
  907.         print '-'*72
  908.         for name,category in categories.items():
  909.             print name, '...'
  910.             lang, enc = getlocale(category)
  911.             print '   Language: ', lang or '(undefined)'
  912.             print '   Encoding: ', enc or '(undefined)'
  913.             print
  914.  
  915. ###
  916.  
  917. try:
  918.     LC_MESSAGES
  919. except NameError:
  920.     pass
  921. else:
  922.     __all__.append("LC_MESSAGES")
  923.  
  924. if __name__=='__main__':
  925.     print 'Locale aliasing:'
  926.     print
  927.     _print_locale()
  928.     print
  929.     print 'Number formatting:'
  930.     print
  931.     _test()
  932.